# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: class_structure1.py 
@time: 2019/6/19 16:23
"""
'''
简化数据结构的初始化，省去烦人的__init__()初始化函数
'''
import math

class Structure1:
    # Class variable that specifies expected fields
    _fields = []
    def __init__(self, *args):
        if len(args) != len(self._fields):
            raise TypeError('Expected {} arguments'.format(len(self._fields)))
        # set the arguments
        for name, value in zip(self._fields,args):
            setattr(self, name, value)


# Example use
class Stock(Structure1):
    _fields = ['name', 'shares', 'price']

    def __repr__(self):
        return "{},{},{}".format(self.name,self.shares, self.price)

class Point(Structure1):
    _fields = ['x', 'y']

    def __str__(self):
        return '({},{})'.format(self.x, self.y)

class Circle(Structure1):
    _fields = ['radius']

    def area(self):
        return math.pi * self.radius ** 2

s = Stock('Tom', 50, 91.01)
print(s)

p = Point(34.6443, 65.3245)
print(p)

c = Circle(2)
print(c.area())
